var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({

  data: {
    imgUrl: globalData.imgUrl,
    titleOne: "商家类型",
    titleTwo: "距离",
    titleZro: "区域",
    selectListOne: [],
    selectListTwo: ["1000米以内", "2000米以内", "5000米以内", "10000米以内"],//1:1000 2:2000 3:5000 4:10000
    selectListZro: ["南山","宝安"],
    showId:-1,
    businessType:'',//类型id
    areaId:'',
    pageNo:1,
    pageSize:10,
    lat:'',
    lon:'',
    list:[],
    totalPage:1,//总页数
    banners:[],
  },
  loadData: function () {
    this.banners();
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({ lat: res.latitude })
        that.setData({ lon: res.longitude })
        that.list();
        that.quyu();
      },
      fail: function (res) {
        console.log('拒绝获取经纬度');
        that.list();
        that.quyu();
      }
    })
    this.types();
  },
  
  list:function(){
    var params = new Object();
    params.lon = this.data.lon;
    params.lat = this.data.lat;
    params.pageNo = this.data.pageNo;
    params.pageSize = this.data.pageSize;
    params.businessType = this.data.businessType;
    params.areaId = this.data.areaId;//地区id
    let titleTwo = this.data.titleTwo;
    switch (titleTwo) {
      case '1000米以内':params.distanceType = 1;break;
      case '2000米以内': params.distanceType = 2; break;
      case '5000米以内': params.distanceType = 3; break;
      case '10000米以内': params.distanceType = 4; break;
      // default:
    }
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    console.log("param:"+JSON.stringify(params));
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/business/getShopList",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("res：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data.list;
          that.setData({ totalPage: res.data.totalPage})
          if (that.data.pageNo == 1) {
            that.setData({ list: rows })
          } else {
            var list = that.data.list;
            list = list.concat(rows);
            that.setData({ list: list })
          }
        }
      }
    })
    
  },
  //商家类型
  types: function () {
    var that = this;
    var params = new Object();
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/business/getBusinessType",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("类型：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data;
          that.setData({ selectListOne: rows })
        }
      }
    })
  },
  //设置显示块id
  showSearch: function (e) {
    let id = e.currentTarget.id;
    if (id == this.data.showId) {
      this.setData({ showId: -1, })
    } else {
      this.setData({ showId: id })
    }
    console.log(this.data.showId)
  },
  //查询条件选择事件
  selectItem: function (e) {
    let text = e.currentTarget.dataset.text;
    let id = e.currentTarget.id;
    if (id == 1) { //text为index,-1为全部
      if (text == '-1') {
        this.setData({ titleOne: '全部', businessType: '' })
      } else {
        let index = Number(text);
        let rows = this.data.selectListOne;
        let name = rows[index].name;
        let typeId = rows[index].id;
        this.setData({ titleOne: name, businessType: typeId, })
      }
    } else if (id == 2) {
      if (text == '-1') {
        this.setData({ titleTwo: '全部'})
      } else {
        this.setData({ titleTwo: text })
      }
    } else if (id == 0) { //条件text为index,-1为全部
      if (text == '-1') {
        this.setData({ titleZro: '全部', areaId: '' })
      } else {
        let index = Number(text);
        let rows = this.data.selectListZro;
        let areaname = rows[index].areaname;
        let areaid = rows[index].areaid;
        this.setData({ titleZro: areaname, areaId: areaid, })
      }
    }
    this.setData({ showId: -1 })
    this.list();
  },
  //区域
  quyu: function () {
    var that = this;
    var params1 = new Object();
    params1.lon = this.data.lon;
    params1.lat = this.data.lat;
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/business/getPosArea",
      dataType: "json",
      method: "GET",
      data: params1,
      success: function (res) {
        console.log("城市：" + JSON.stringify(res));
        res = res.data;
        if (res.result == 0) {
          var params = new Object();
          if (res.data.city.areaid != undefined){
            let parentId = res.data.city.areaid;
            params.parentId = parentId;//父级区域ID
          }
          console.log("param：" + JSON.stringify(params));
          wx.request({
            header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
            url: httpsUrl + "/business/getAreaList",
            dataType: "json",
            method: "GET",
            data: params,
            success: function (res) {
              console.log("区域：" + JSON.stringify(res));
              res = res.data;
              if (res.result == 0) {
                let rows = res.data;
                that.setData({ selectListZro: rows })
              }
            }
          })
        }
      }
    })
  },
  shopInfo:function(e){
    let index = e.currentTarget.id;
    let list = this.data.list;
    wx.setStorageSync("businessCode", list[index].code)
    wx.navigateTo({
      url: '../shopInfo/shopInfo',
    })
  },
  //顶部大图 
  banners: function () {
    var that = this;
    var params = new Object();
    params.advSite = 3;//广告位置 "1":首页banner大图, "2":首页文字公告, "3":商家页banner大图, "4":海报页banner大图	
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/adver/getPosAdverList",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("大图：" + JSON.stringify(res));
        if (res.result == 0) {
          // let rows = res.data;
          that.setData({ banners: res.data })
        }
      }
    })
  },
  //广告跳转 link_type：1,链接到商家首页 2,链接到商品页  link_data：参数code
  bannerGo: function (e) {
    let id = e.currentTarget.id;
    let banners = this.data.banners;
    let banner = banners[id];
    if (banner.link_type == 1) {
      wx.setStorageSync("businessCode", banner.link_data);
      wx.navigateTo({
        url: '../../shop/shopInfo/shopInfo',
      })
    } else if (banner.link_type == 2) {
      wx.setStorageSync("goodsCode", banner.link_data);
      wx.navigateTo({ url: '../../shop/goodsInfo/goodsInfo', })
    }
  },
  onLoad: function (options) {
    this.loadData();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ pageNo:1});
    this.loadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNo = this.data.pageNo;
    pageNo = pageNo + 1;
    if (pageNo > this.data.totalPage) {
      commonUtil.showSuccessToast("没有更多啦~");
      return;
    } else {
      this.setData({ pageNo: pageNo })
      this.list();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})